package com.bdweb.controller;

import com.bdweb.model.TASK;
import com.bdweb.pojo.Result;

import com.bdweb.service.impl.TaskServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class taskController {


    @Autowired
    private TaskServiceImpl taskService;

    @CrossOrigin(origins = "*")   //设置跨域

    // 添加数据到数据库
    @PostMapping("/task")
    public Result postTask(TASK task_data) {
        System.out.println(task_data);
        taskService.addTask(task_data);

        return new Result(200, "success", task_data);
    }

    @CrossOrigin(origins = "*")   //设置跨域
    // 获取所有任务数据
    @GetMapping("/task/list")
    public Result getTaskList() {

        List<TASK> allTask = taskService.findAllTask();
         if(allTask!=null)
        for (int i = 0; i < allTask.size(); i++) {
            String start = allTask.get(i).getStart1().replace('T', ' ');
            allTask.get(i).setStart1(start);
            String end = allTask.get(i).getEnd1().replace('T', ' ');
            allTask.get(i).setEnd1(end);

        }



        return new Result(200, "success", allTask);
    }


    // 根据id删除数据库内容
    @CrossOrigin(origins = "*")   //设置跨域
    @GetMapping("/deleteList")
    public Result getTaskList(@RequestParam(name="id") Integer id) {
        System.out.println(id);
        Integer integer = taskService.deleteTask(id);
        return new Result(200, "success", integer);
    }
}
